package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/***
 *                    _ooOoo_
 *                   o8888888o
 *                   88" . "88
 *                   (| -_- |)
 *                    O\ = /O
 *                ____/`---'\____
 *              .   ' \\| |// `.
 *               / \\||| : |||// \
 *             / _||||| -:- |||||- \
 *               | | \\\ - /// | |
 *             | \_| ''\---/'' | |
 *              \ .-\__ `-` ___/-. /
 *           ___`. .' /--.--\ `. . __
 *        ."" '< `.___\_<|>_/___.' >'"".
 *       | | : `- \`.;`\ _ /`;.`/ - ` : | |
 *         \ \ `-. \_ __\ /__ _/ .-` / /
 * ======`-.____`-.___\_____/___.-`____.-'======
 *                    `=---='
 *
 * .............................................
 *          佛祖保佑             永无BUG
 */

@RestController
@RequestMapping("role")
public class RoleController {

    @Reference
    private RoleService roleService;

    @RequestMapping("findRole")
    public Result findRole(@RequestBody QueryPageBean queryPageBean) {
        try {
            PageResult pageResult = roleService.findRole(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
            return new Result(true, "查询角色列表成功", pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询角色列表失败");
        }
    }

    @RequestMapping("edit")
    public Result edit(@RequestParam("menuIds") Integer[] menuIds, @RequestParam("permissionIds") Integer[] permissionIds, @RequestBody Role role) {
        try {
            roleService.edit(menuIds, permissionIds, role);
            return new Result(true, "修改角色信息成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "修改角色信息失败");
        }
    }

    @RequestMapping("deleteRoleById")
    public Result deleteRoleById(@RequestParam("id") Integer id) {
        try {
            roleService.deleteRoleById(id);
            return new Result(true, "删除角色信息成功");
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除角色信息失败");
        }
    }

    @RequestMapping("add")
    public Result add(@RequestParam("menuIds") Integer[] menuIds, @RequestParam("permissionIds") Integer[] permissionIds, @RequestBody Role role) {
        try {
            roleService.add(menuIds, permissionIds, role);
            return new Result(true, "添加角色信息成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "添加角色信息失败");
        }
    }

    @RequestMapping("findRoleAll")
    public Result findRoleAll() {
        try {
            List<Role> roleList = roleService.findRoleAll();
            return new Result(true, "查询角色信息成功", roleList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询角色信息失败");
        }
    }

    @RequestMapping("findRoleByUserId")
    public Result findRoleByUserId(@RequestParam("id") Integer id) {
        try {
            List<Integer> roleList = roleService.findRoleByUserId(id);
            return new Result(true, "查询角色信息成功", roleList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询角色信息失败");
        }
    }
}

/***
 *                    .::::.
 *                  .::::::::.
 *                 ::::::::::: 
 *             ..:::::::::::'
 *           '::::::::::::'
 *             .::::::::::
 *        '::::::::::::::..
 *             ..::::::::::::.
 *           ``::::::::::::::::
 *            ::::``:::::::::'        .:::.
 *           ::::'   ':::::'       .::::::::.
 *         .::::'      ::::     .:::::::'::::.
 *        .:::'       :::::  .:::::::::' ':::::.
 *       .::'        :::::.:::::::::'      ':::::.
 *      .::'         ::::::::::::::'         ``::::.
 *  ...:::           ::::::::::::'              ``::.
 * ```` ':.          ':::::::::'                  ::::..
 *                    '.:::::'                    ':'````..
 */